package sf.database.jdbc.handle;

import sf.database.jdbc.rowmapper.RowMapper;
import sf.database.jdbc.sql.ResultSetCallback;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class SingleRowHandler<T> implements ResultSetCallback<T> {

    private RowMapper<T> mapper;

    public SingleRowHandler(RowMapper<T> mapper) {
        this.mapper = mapper;
    }

    @Override
    public T callback(ResultSet rs) throws SQLException {
        ResultSetMetaData rsmd = rs.getMetaData();
        if (rs.next()) {
            return mapper.handle(rs, rsmd, 0);
        }
        return null;
    }
}
